/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { mergeWith, uniq } from 'lodash';
import { useState } from 'react';

interface UpdateItem {
  add: string[];
  remove: string[];
}

interface UpdateData {
  [k: string]: UpdateItem;
}

export default () => {
  const [data, setData] = useState({});

  function update(cur: UpdateData) {
    setData((pre) => {
      const result = mergeWith(pre, cur, (pItem = [], cItem) => {
        const list: string[] = pItem;
        const nList = uniq(list.concat(cItem.add).filter((item) => !cItem.remove.includes(item)));
        return nList.filter((item) => !!item);
      });
      return {
        ...result,
      };
    });
  }

  return {
    data,
    update,
  };
};
